// packageUser/pages/auth/auth-detail.js
import WxValidate from '../../../plugins/wx-validate/WxValidate';
import UserService from '../../../service/UserService';
import CommonService from '../../../service/CommonService'
import { throttle } from '../../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    sjFiles:[], 
    zmFiles:[],
    validDisabled: false,
    validTxt: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userService = new UserService();
    this.commonService = new CommonService();
    const user = app.globalData.user, zmFiles = [], sjFiles = [];
    for (let i = 0; i < user.zmFiles.length; i++) {
      zmFiles.push(user.zmFiles[i].fileUrl)
    }
    for (let i = 0; i < user.sjFiles.length; i++) {
      sjFiles.push(user.sjFiles[i].fileUrl)
    }
    this.setData({ user: app.globalData.user, sjFiles, zmFiles });
  },
  onShow: function(){
    const { chooseCity } = app.globalData;
    const { user } = this.data;
    if (chooseCity.id) {
      user.city = chooseCity;
      user.adcode = chooseCity.id;
      this.setData({ user });
    }
  },
  setFieldValue: function (field, value) {
    const { user } = this.data;
    user[field] = value;
    this.setData({ user });
  },
  onPhoneChange: function(e){
    this.setFieldValue("phone", e.detail.value);
  },
  /**
   * 修改认证手机
   */
  modifyPhone: function(e){
    const { user } = this.data;
    if (!user.phone){
      this.setData({ errMsg: '请输入手机号' });
      return;
    }
    if (!user.validCode){
      this.setData({ errMsg: '请输入验证码' });
      return;
    }
    this.setData({ errMsg: '' })
    const param = {id: user.id, phone: user.phone, validCode: user.validCode};
    this.userService.modifyPhone(param).then(function (res) {
      console.log(res);
    });
  },
  /**
   * 修改联系人
   */
  modifyLink: function(e){
    const { user } = this.data;
    if (!user.factoryName) {
      this.setData({ errMsg: '请输入工厂名称' });
      return;
    }
    if (!user.trueName) {
      this.setData({ errMsg: '请输入责任人名称' });
      return;
    }
    if (!user.address) {
      this.setData({ errMsg: '请输入工厂地址' });
      return;
    }
    if (!user.address) {
      this.setData({ errMsg: '请上传身份证正面照片' });
      return;
    }
    if (!user.address) {
      this.setData({ errMsg: '请上传身份证背面照片' });
      return;
    }
    this.setData({ errMsg: '' });
    const param = {
      id: user.id,
      factoryName: user.factoryName,
      trueName: user.trueName,
      adcode: user.adcode,
      lat: user.lat,
      lng: user.lng,
      address: user.addres
    }
    this.userService.modifyLink(param).then(function (res) {
      console.log(res);
    });
  },
  modifyBank: function(e){
    const { user } = this.data;
    if (!user.bankName) {
      this.setData({ errMsg: '请输入银行名称' });
      return;
    }
    if (!user.bankUser) {
      this.setData({ errMsg: '请输入收款人' });
      return;
    }
    if (!user.bankNum) {
      this.setData({ errMsg: '请输入银行帐户' });
      return;
    }
    if (!user.bankAddr) {
      this.setData({ errMsg: '请输入开户银行' });
      return;
    }
    this.setData({ errMsg: '' });
    const param = {
      id: user.id,
      bankName: user.bankName,
      bankUser: user.bankUser,
      bankNum: user.bankNum,
      bankAddr: user.bankAddr
    }
    this.userService.modifyBank(param).then(function (res) {
      console.log(res);
    });
  },
  modifyZm: function(e){
    const { user } = this.data;
    if (!user.zmFiles || user.zmFiles.length < 3) {
      this.setData({ errMsg: '资质图片至少上传3张' })
      return false;
    }
    if (user.zmFiles && user.zmFiles.length > 9) {
      this.setData({ errMsg: '资质图片至多上传9张' })
      return false;
    }
    this.setData({ errMsg: '' });
    const param = {
      id: user.id,
      zmFiles: user.zmFiles,
    }
    this.userService.modifyZm(param).then(function (res) {
      console.log(res);
    });
  },
  modifySj: function(e){
    const { user } = this.data;
    if (!user.sjFiles || user.sjFiles.length < 3) {
      this.setData({ errMsg: '实景图片至少上传3张' })
      return false;
    }
    if (user.sjFiles && user.sjFiles.length > 9) {
      this.setData({ errMsg: '实景图片至多上传9张' })
      return false;
    }
    this.setData({ errMsg: '' });
    const param = {
      id: user.id,
      sjFiles: user.sjFiles,
    }
    this.userService.modifySj(param).then(function (res) {
      console.log(res);
    });
  },
  /**获取短信验证码 */
  onValidCode: function (e) {
    const _ = this;
    const { user } = this.data;
    if (!user.phone) {
      this.setData({ errMsg: '手机号码不能为空' })
      return;
    }
    this.setData({ validDisabled: true, errMsg: '' })
    this.commonService.getValidCode(user.phone).then(res => {
      console.log(res);
      if (res.status == 0) {
        let count = 60;
        const t = setInterval(() => {
          const { validTxt } = _.data;
          count--;
          _.setData({ validTxt: count });
          if (count < 0) {
            _.setData({ validTxt: '重新获取', validDisabled: false });
            clearInterval(t);
          }
        }, 1000);
      }
    })
  },
  onCode: function (e) {
    this.setFieldValue('validCode', e.detail.value);
  },
  onLocationTap: function () {
    throttle(function (e) {
      wx.navigateTo({
        url: '/pages/citys/citys'
      })
    }, 1000)();
  },
  onLocation: function (e) {
    const _ = this;
    const { user } = this.data;
    wx.chooseLocation({
      success: function (res) {
        user.address = res.address;
        user.lng = res.longitude;
        user.lat = res.latitude;
        _.setData({ user });
      }
    })
  },
  onFactoryNameChange: function (e) {
    this.setFieldValue('factoryName', e.detail.value);
  },
  onTrueNameChange: function (e) {
    this.setFieldValue('trueName', e.detail.value);
  },
  onAddressChange: function (e) {
    this.setFieldValue('address', e.detail.value);
  },
  onCardFront: function (e) {
    this.setFieldValue("cardFrontImg", e.detail.value);
  },
  onCardBack: function (e) {
    this.setFieldValue("cardBackImg", e.detail.value);
  },

  onZmImagesChange: function (e) {
    const images = e.detail.images.map(fileUrl => {
      return { fileUrl, fileType: 3 };
    });
    this.setFieldValue("zmFiles", images);
  },
  onSjImagesChange: function (e) {
    const images = e.detail.images.map(fileUrl => {
      return { fileUrl, fileType: 4 };
    });
    this.setFieldValue("sjFiles", images);
  },
  onBankNameChange: function (e) {
    this.setFieldValue('bankName', e.detail.value);
  },
  onBankUserChange: function (e) {
    this.setFieldValue('bankUser', e.detail.value);
  },
  onBankNumChange: function (e) {
    this.setFieldValue('bankNum', e.detail.value);
  },
  onBankAddrChange: function (e) {
    this.setFieldValue('bankAddr', e.detail.value);
  },
})